//单向链表
public class MySingleLinkedList implements ISingleLinkedList {

    static class ListNode {
        //节点数据
        public int val;

        //节点的引用
        public ListNode next;

        //数据val的构造方法
        public ListNode(int val) {
            this.val = val;
        }
    }

    //储存链表头节点的引用
    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        head = node1;
    }


    //------------------------------------单向链表实现-------------------------------------------------

    //头插法
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        node.next = head;
        head = node;

    }


    //尾插法
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        //1.如果该链表为空 此时插入的节点 就是第一个节点
        if (head == null) {
            head = node;
            return;
        }

        //2.遍历找到链表的尾巴
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }

        //3.此时指向的节点 为插入的尾巴节点
        cur.next = node;

    }

    //任意位置插入,第一个数据节点为0号下标
    @Override
    public void addIndex(int index, int data) {
        //1.判断插入的下标合不合法
        checkPos(index);

        //如果节点为空 则为头插法
        if (index == 0) {
            addFirst(data);
            return;
        }

        //如果节点为最后一位 则进行尾插法
        if (index == size()) {
            addLast(data);
            return;
        }

        //2.中间位置进行插入
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);

        node.next = cur.next;
        cur.next = node;


    }

    //通过下标找到插入节点指向的节点
    public ListNode findIndex(int index) {
        ListNode cur = head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;

    }



    private void checkPos(int index) {
        if (index < 0 || index > size()) {
            throw new CheckPosException("index位置不合法: "+index);
        }
    }

    //查找是否包含关键字key是否在单链表当中
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    @Override
    public void remove(int key) {
        //判断链表是否为空
        if (head == null) {
            System.out.println("链表为空！");
            return;
        }

        //判断key是否为链表的第一个节点
        if (head.val == key) {
            head = head.next;
            return;
        }

        //找到要删除的节点的前一个节点
        ListNode cur = search(key);
        if (cur == null) {
            System.out.println("没有你要删除的数字："+key);
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;


    }

    //找到要删除的key的前一个节点
    private ListNode search(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }


    //删除所有值为key的节点
    @Override
    public void removeAllKey(int key) {
        if (head == null){
            return;
        }

        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null){
            if (cur.val == key){
                pre.next = cur.next;
            }else {
                pre = cur;
            }
            cur = cur.next;
        }

        if (head.val == key){
            head = head.next;
        }
    }

    //得到单链表的长度
    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //打印链表
    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        head = null;
    }


    //---------------------------链表OJ题--------------------------

    //反转链表
    public ListNode reverseList() {
        //1.判断链表是否为空
        if(head == null) {
            return null;
        }

        //2.判断链表是否只有一个节点
        if(head.next == null) {
            return head;
        }

        //3.链表节点两个及以上
        ListNode cur = head.next;
        //手动将head的指向设为null 因为反转后第一个节点将不指向任何任何节点
        head.next = null;

        while(cur != null) {
            //3.1 先把cur的指向存储在curN
            ListNode curN = cur.next;

            //3.2 进行头插法
            cur.next = head;
            head = cur;

            //3.3 再把curN赋值给cur
            cur = curN;
        }

        return head;

    }

    //链表的中间节点
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }

    //返回链表倒数第K个节点
    public int kthToLast(int k) {
        if (k <= 0 || head == null || k > size()) {
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(k-1 != 0){
            fast = fast.next;
            k--;
        }

        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }

        return slow.val;

    }

    //合并两个有序链表
    public ListNode mergeTwoLists(ListNode headA, ListNode headB) {

        ListNode newH = new ListNode(1);
        ListNode tmp = newH;

        if (headA.val < headB.val) {
            tmp.next = headA;
            headA = headA.next;
        }else {
            tmp.next = headB;
            headB = headB.next;
        }

        if(headA.next != null) {
            tmp.next = headA;
        }

        if(headB.next != null) {
            tmp.next = headB;
        }

        return newH.next;
    }

    //链表的回文结构
    public boolean chkPalindrome(ListNode head) {
        // write code here
        //1.找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.slow此时指向了中间位置 开始反转后半部分
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //3.此时head和cur一直走 直到相遇
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    //链表分割
    public ListNode partition(ListNode head, int x) {
        // write code here
        if(head == null){
            return null;
        }

        //第一段节点 小于x
        ListNode bs = null; //头节点
        ListNode be = null; //尾节点

        //第二段节点 大于等于x
        ListNode as = null; //头节点
        ListNode ae = null; //尾节点

        ListNode cur = head;
        while(cur != null){
            //小于x
            if(cur.val < x){
                if(bs == null){
                    bs = be = cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else{
                //大于x
                if(as == null){
                    as = ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }

        //判断第一段节点是否为空 空则返回第二段节点的as
        if(bs == null){
            return as;
        }

        //有两段节点的情况下 则进行连接
        be.next = as;

        //如果第二段节点不为空 把第二段的尾节点设为null
        if(as != null){
            ae.next = null;
        }

        return bs;

    }

    //相交链表
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode PL = headA;
        ListNode PS = headB;

        int lenA = 0;
        int lenB = 0;

        while(PL != null){
            lenA++;
            PL = PL.next;
        }

        while(PS != null){
            lenB++;
            PS = PS.next;
        }

        PL = headA;
        PS = headB;

        int len = lenA - lenB;
        if(len < 0){
            PL = headB;
            PS = headA;
            len = lenB - lenA;
        }

        while(len != 0){
            PL = PL.next;
            len--;
        }

        while(PL != PS){
            PL = PL.next;
            PS = PS.next;
        }

        return PL;

    }


    //环形链表
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }

    //环形链表II


}
